
package maa.filter;


import maa.entity.Role;
import maa.entity.User;
import org.apache.log4j.Logger;

import javax.servlet.*;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.util.EnumSet;
import java.util.HashMap;
import java.util.Map;

public class SecurityFilter implements Filter {
    private static final Logger logger = Logger.getLogger(SecurityFilter.class);
    private Map<String, EnumSet<Role>> actions = new HashMap<String, EnumSet<Role>>();


    @Override
    public void init(FilterConfig filterConfig) throws ServletException {
        EnumSet<Role> all = EnumSet.of(Role.VISITOR,
                Role.ADMIN, Role.MENTOR, Role.CLIENT);
        EnumSet<Role> authorized = EnumSet.of(Role.ADMIN,
                Role.MENTOR, Role.CLIENT);
        EnumSet<Role> users = EnumSet.of(Role.MENTOR, Role.CLIENT);
        EnumSet<Role> administrator = EnumSet.of(Role.ADMIN);
        EnumSet<Role> mentor = EnumSet.of(Role.MENTOR);
        EnumSet<Role> client = EnumSet.of(Role.CLIENT);
        EnumSet<Role> visitor = EnumSet.of(Role.VISITOR);
//All
        actions.put("GET/main", all);
        actions.put("GET/index", all);
        actions.put("GET/courses", all);
        actions.put("GET/mentors", all);
        actions.put("POST/change-locale", all);
        actions.put("GET/successful-registration", all);

//Visitors
        actions.put("GET/login-form", visitor);
        actions.put("GET/registration-form", visitor);
        actions.put("POST/login", visitor);
        actions.put("POST/register", visitor);

//Authorized
        actions.put("POST/logout", authorized);
        actions.put("GET/success", authorized);
        actions.put("POST/update-user", authorized);

//Users
        actions.put("GET/account", users);
        actions.put("GET/user-self-update", users);
        actions.put("POST/edit-profile", users);

//Client
        actions.put("POST/subscribe", client);
        actions.put("POST/unsubscribe", client);
        actions.put("GET/my-subscriptions", client);

//Mentor
        actions.put("GET/mycourses", mentor);

//Admin
        actions.put("GET/course-creation", administrator);
        actions.put("POST/create-course", administrator);
        actions.put("GET/admin-cabinet", administrator);
        actions.put("GET/user-creation", administrator);
        actions.put("POST/create-user", administrator);
        actions.put("GET/view-courses", administrator);
        actions.put("GET/view-users", administrator);
        actions.put("GET/hide-courses", administrator);
        actions.put("GET/hide-users", administrator);
        actions.put("POST/remove-course", administrator);
        actions.put("POST/remove-user", administrator);
        actions.put("POST/user-update-page", administrator);
        actions.put("POST/course-update-page", administrator);
        actions.put("GET/user-update", administrator);
        actions.put("POST/update-course", administrator);
        actions.put("GET/course-update", administrator);
        actions.put("POST/mentor-creation-page", administrator);
        actions.put("GET/mentor-creation", administrator);
        actions.put("POST/create-mentor", administrator);
        actions.put("GET/view-mentors", administrator);
        actions.put("GET/hide-mentors", administrator);
        actions.put("POST/mentor-update-page", administrator);
        actions.put("POST/mentor-update", administrator);
        actions.put("GET/mentor-update", administrator);
        actions.put("POST/remove-mentor", administrator);
        actions.put("POST/choose-mentor", administrator);
        actions.put("GET/choose-mentor", administrator);
        actions.put("POST/appoint-mentor", administrator);
        actions.put("GET/view-groups-dash", administrator);
        actions.put("GET/view-groups", administrator);
        actions.put("POST/restore-mentor", administrator);
        actions.put("GET/show-log", administrator);
    }

    @Override
    public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
        HttpServletRequest request = (HttpServletRequest) servletRequest;
        HttpServletResponse response = (HttpServletResponse) servletResponse;
        response.setCharacterEncoding("UTF-8");
        HttpSession session = request.getSession();
        String action = request.getMethod() + "/" + request.getParameter("action");
        EnumSet<Role> group = actions.get(action);
        User user = (User) session.getAttribute("user");
        Role role = Role.VISITOR;

        if (user != null) role = user.getRole();
        if (group == null || group.contains(role)) {
            logger.debug("Access allowed.");
            filterChain.doFilter(request, response);
            return;
        } else {
            logger.debug("Access denied, forwarded to main page.");
            Integer statusCode = 403;
            request.setAttribute("errorCode", statusCode);
            request.getRequestDispatcher("/WEB-INF/jsp/error.jsp").forward(request, response);
        }
    }

    public void destroy() {
    }

}
